package joney.main.action;

import java.util.Date;

import joney.main.utils.JoneyUtil;
import joney.model.Account;
import joney.model.DataModel;
import joney.model.Expense;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

public class AccountActions {

	public static final String NEW_ACCOUNT = "New Account";
	public static final String DELETE_ACCOUNT = "Delete Account";
	public static final String OFFSET_ACCOUNT = "Offset Account";
	public static final String CLEAR_ALL = "Clear All Accounts";
	
	public static class ClearAll extends AbstractAction
	{
		public ClearAll(IWorkbenchWindow window)
		{
			super(window, "&" + CLEAR_ALL, CLEAR_ALL);
		}
		
		
		public void run() {
			
			if(MessageDialog.openConfirm(window.getShell(), "Are you sure?", "Clear All Accounts\nData will be lost"))
				DataModel.clearAllState();
			
		}
		
		
	}
	
	
	
	public static class NewAccount extends AbstractAction
	{
		public NewAccount(IWorkbenchWindow window) {
			super(window, "&" + NEW_ACCOUNT, NEW_ACCOUNT);
		}

		public void run() {
			
			final Shell shell = new Shell(window.getShell());
			shell.setSize(400, 400);
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			shell.setLayout(gridLayout);
			shell.setText("New Account");
			
			Label name = new Label(shell, SWT.None);
			name.setText("Account");
			final Text tname = new Text(shell, SWT.SINGLE);
			tname.setText("NoNameAccount");
			
			final Label startingAmount = new Label(shell, SWT.None);
			startingAmount.setText("Starting Amount");
			final Text amount = new Text(shell, SWT.SINGLE);
			amount.setText("0");
			
			Button add = new Button(shell, SWT.PUSH);
			add.setText("Add");
			add.addSelectionListener(new SelectionAdapter()
			{
				public void widgetSelected(SelectionEvent e) {
					
					Account a = new Account(tname.getText(), Double.valueOf(amount.getText()));
					DataModel.accounts.add(a);
					DataModel.fireAccountAdded(a);
					
				}
				
			});
			
	
			
			
			  shell.pack();
			  shell.open();
		}
		
	}
	
	public static class DeleteAccount extends AbstractAction
	{
		public DeleteAccount(IWorkbenchWindow window) {
			super(window, "&"+DELETE_ACCOUNT, DELETE_ACCOUNT);
		}

		public void run() {
			final Shell shell = new Shell(window.getShell());
			shell.setSize(400, 400);
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			shell.setLayout(gridLayout);
			shell.setText("Delete");
			
			Label from = new Label(shell, SWT.None);
			from.setText("Account");
			final Combo cb = new Combo(shell, SWT.DROP_DOWN | SWT.BORDER);
			for(Account a: DataModel.accounts)
				cb.add(a.getName());
			
			
			  shell.pack();
			  shell.open();
		}
		
	}
	
	public static class OffsetAccount extends AbstractAction
	{
		public OffsetAccount(IWorkbenchWindow window) {
			super(window, "&"+OFFSET_ACCOUNT, OFFSET_ACCOUNT);
		}

		public void run() {
			
			final Shell shell = new Shell(window.getShell());
			shell.setSize(400, 400);
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			shell.setLayout(gridLayout);
			shell.setText("Offset");
			
			Label from = new Label(shell, SWT.None);
			from.setText("Account");
			final Combo cb = new Combo(shell, SWT.DROP_DOWN | SWT.BORDER);
			for(Account a: DataModel.accounts)
				cb.add(a.getName());
			
			
			Label l = new Label(shell, SWT.None);
			l.setText("Amount");
			final Text amount = new Text(shell, SWT.SINGLE);
			amount.addListener(SWT.Verify, new Listener() { //add numeric only
		           public void handleEvent(Event e) {
		              String string = e.text;
		              char[] chars = new char[string.length()];
		              string.getChars(0, chars.length, chars, 0);
		              for (int i = 0; i < chars.length; i++) {
		                if (!('0' <= chars[i] && chars[i] <= '9') && chars[i] != '.' && chars[i] != '-') {
		                  e.doit = false;
		                return;
		               }
		              }
		            }
	         });
			
			 Button show = new Button(shell, SWT.PUSH);
			    show.setText("Offset");
			    show.addSelectionListener(new SelectionAdapter() {
			      public void widgetSelected(SelectionEvent event) {

			    	  String acc = cb.getText();
			    	  if(acc.isEmpty()){return;}
			    	  
			    	  for(Account a: DataModel.accounts)
			    	  {
			    		  if(acc.equals(a.getName()))
			    		  {
			    			  a.addStartingAmount(Double.valueOf(amount.getText()));
			    		  }
			    	  }
			    	  
			    	  DataModel.isDirty= true;
			    	  DataModel.fireExpensesUpdated();
			    	  
			      }});
			
		    shell.pack();
		    shell.open();
		}
		
	}
	
	public static class TransferAmount extends AbstractAction
	{

		public TransferAmount(IWorkbenchWindow window) {
			super(window, "&" + Expense.TRANSFER, Expense.TRANSFER);
			setCtrlAcceleratorKey('T');
		}

		public void run() {
			final Shell shell = new Shell(window.getShell());
			shell.setSize(400, 400);
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			shell.setLayout(gridLayout);
			shell.setText("Transfer");
			
			Label from = new Label(shell, SWT.None);
			from.setText("From");
			final Combo cb = new Combo(shell, SWT.DROP_DOWN | SWT.BORDER);
			for(Account a: DataModel.accounts)
				cb.add(a.getName());
			
			Label to = new Label(shell, SWT.None);
			to.setText("To");
			final Combo cb2 = new Combo(shell, SWT.DROP_DOWN | SWT.BORDER);
			for(Account a: DataModel.accounts)
				cb2.add(a.getName());
			
			Label l = new Label(shell, SWT.None);
			l.setText("Amount");
			final Text amount = new Text(shell, SWT.SINGLE);
			amount.addListener(SWT.Verify, new Listener() { //add numeric only
		           public void handleEvent(Event e) {
		              String string = e.text;
		              char[] chars = new char[string.length()];
		              string.getChars(0, chars.length, chars, 0);
		              for (int i = 0; i < chars.length; i++) {
		                if (!('0' <= chars[i] && chars[i] <= '9') && chars[i] != '.') {
		                  e.doit = false;
		                return;
		               }
		              }
		            }
	         });
			
			 Button show = new Button(shell, SWT.PUSH);
			    show.setText(Expense.TRANSFER);
			    show.addSelectionListener(new SelectionAdapter() {
			      public void widgetSelected(SelectionEvent event) {

			    	  if(cb.getText().equals(cb2.getText())){return;}
						
						Expense to = 
							new Expense(new Date().getTime(), 
								Expense.TRANSFER,
								"<- " + cb2.getText(), "", 
								Double.valueOf(amount.getText()));
						
			    		Expense from = 
			    			new Expense(new Date().getTime(), 
			    					Expense.TRANSFER,
									"-> " + cb.getText(), "", 
									-Double.valueOf(amount.getText()));
			    
			    		
			    		DataModel.fireExpenseAdded(from, cb.getText());
			    		DataModel.fireExpenseAdded(to, cb2.getText());
						DataModel.fireTransfeOccured(cb.getText(), cb2.getText(), from, to);
						
						shell.close();
			    	  
			      }});
			
		    shell.pack();
		    shell.open();
		}
			
		}
	
}
